import axios from 'axios'
import * as constants from './constants'
import { fromJS } from 'immutable';

export const initData = (data) => ({
    type: constants.INIT_LIST,
    data: fromJS(data)
});

export const getDetail = (data) => ({
    type: constants.GET_DETAIL,
    data: fromJS(data)
});

export const changeInputValue = (data) => ({
    type: constants.CHANGE_INPUT_VALUE,
    value: fromJS(data)
});

export const addItem = () => ({
    type: constants.ADD_ITEM
});

export const delItem = (data) => ({
    type: constants.DELETE_ITEM,
    index: fromJS(data)
});

// axios.defaults.baseURL = 'http://rap2api.taobao.org/app/mock/104603/';
axios.defaults.baseURL = 'https://gank.io/api/';

export const getList = () => {
    return (dispatch) => {
        axios.get('data/%E7%A6%8F%E5%88%A9/10/1')
            .then((response) => {
                const data = response.data;
                const action = initData(data.results);
                dispatch(action);
            }).catch(() => {
                console.log("error")
            });
    }
};

export const initDetail = (date) => {
    return (dispatch) => {
        axios.get('day/' + date)
            .then((response) => {
                const data = response.data;
                // console.log(data);
                // console.log(data.results);
                const action = getDetail(data.results);
                dispatch(action);
            }).catch(() => {
                console.log("error")
            });
    }
};